#include <stdio.h>
#include <string.h>
#include <netinet/in.h>

typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;

typedef struct NET_CMD {
	u8 cmd;
	u32 pack_id;
} Net_cmd, *pNet_cmd;

typedef struct TFTP_DATA{
	u8 opt;
	u8 filename[200];
	u32 id;
	u32 length;
	u8 data[1500];
}Tftp_data, *pTftp_data;

u32 data_length = 0;
u32 pack_ID = 1;

int main(int argc, char *argv[])
{
	char buf[2000];
	char str[2000];
	int sock_fd, sock_fd2, n, i;
	struct sockaddr_in servaddr, cliaddr;
	socklen_t cliaddr_len;
	pNet_cmd net_cmd = NULL;

	sock_fd = socket(AF_INET,SOCK_DGRAM,0);

	bzero(&servaddr,sizeof(struct sockaddr_in));

	servaddr.sin_family = AF_INET;

	servaddr.sin_port = htons(9000);

	servaddr.sin_addr.s_addr = INADDR_ANY;

	bind(sock_fd,(void *)&servaddr,sizeof(struct sockaddr_in));
	//system("arp -s 192.168.0.91 11:22:33:44:55:66");
	//system("clear");
	//printf("               f-boot\n\n");
	while(1)
	{
		cliaddr_len = sizeof(cliaddr);
		bzero(buf,1024);
		n= recvfrom(sock_fd,buf,sizeof(buf),0,(struct sockaddr *)&cliaddr,&cliaddr_len);
		printf("received from %s at PORT %d\n", (char *)inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));
		printf("%s\n", buf);
	//	if ( ((pNet_cmd)buf)->pack_id != pack_ID)

		for (i = 0; i < n; i++)
			buf[i] = toupper(buf[i]);

		//printf("<uplooking> ");
		//while(1)
			sendto(sock_fd,buf,sizeof(buf),0, (struct sockaddr *)&cliaddr,cliaddr_len);
	}
	return 0;
}

